package com.mty.cc.controller;


import com.mty.cc.po.Course;
import com.mty.cc.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/*
 * @description: 控制层
 * @author: mty
 */
@Controller
@RequestMapping(value = "/course")
class CourseController {


    @Autowired(required=false)
    private CourseService courseService;

    /*
     * @description: 跳转到首页
     * @author: mty
     */
    @RequestMapping("/courseIndex")
    public String courseIndex(HttpServletRequest request,Model model) throws Exception{
        List<Course> courseList = courseService.queryByAll();
        int total = courseList.size();
        HttpSession session = request.getSession();
        String type = session.getAttribute("type").toString();
        model.addAttribute("courseList", courseList);
        model.addAttribute("total", total);
        model.addAttribute("type", type);
        return "course/CourseIndex";
    }

    /*
     * @description:进入修改
     * @author: mty
     */
    @RequestMapping("/courseEdit/{id}")
    public ModelAndView  courseEdit(@PathVariable("id") String id,Model model) throws Exception{
        ModelAndView mv = new ModelAndView();
        Course course = courseService.queryById(id);
        model.addAttribute("course", course);
        mv.setViewName("course/CourseEdit");
        return mv;
    }

    /*
     * @description:修改
     * @author: mty
     */
    @RequestMapping("/courseEditSubmit")
    public String  courseEditSubmit(Course course,Model model,HttpServletRequest request) throws Exception{
        try{
            List<Course> courseList = courseService.queryByAll();
            for(int i = 0;i<courseList.size();i++){
                if(course.getCno().equals(courseList.get(i).getCno())){
                    request.setAttribute("msg", "课程代号重复，请重试！");
                    return "course/CourseAdd";
                }
            }
            courseService.update(course);
            request.setAttribute("msg", "修改成功！");
            return "course/CourseEdit";
        }catch (Exception e){
            request.setAttribute("msg", "修改失败！");
            return "course/CourseEdit";
        }
    }

    /*
     * @description:进入添加
     * @author: mty
     */
    @RequestMapping("/courseAdd")
    public String  courseAdd(Model model,HttpServletRequest request) throws Exception{
        return "course/CourseAdd";
    }

    /*
     * @description:增加
     * @author: mty
     */
    @RequestMapping("/courseAddSub")
    public String  courseAddSub(Course course,Model model,HttpServletRequest request) throws Exception{
        try{
            List<Course> courseList = courseService.queryByAll();
            for(int i = 0;i<courseList.size();i++){
                if(course.getCno().equals(courseList.get(i).getCno())){
                    request.setAttribute("msg", "课程代号重复，请重试！");
                    return "course/CourseAdd";
                }
            }
            courseService.add(course);
            request.setAttribute("msg", "添加成功！");
            return "course/CourseAdd";
        }catch (Exception e){
            request.setAttribute("msg", "添加失败！");
            return "course/CourseAdd";
        }
    }

    /*
     * @description:根据ID删除
     * @author: mty
     */
    @RequestMapping("/courseDel/{id}")
    public ModelAndView courseDel(@PathVariable("id") String id,HttpServletRequest reuqest, Model model) throws Exception{
        ModelAndView mv = new ModelAndView();
        courseService.deleteById(id);
        mv.setViewName("course/CourseIndex");
        return mv;
    }


}